package com.hualan.dao;

import com.hualan.Util.DBUtils;
import com.hualan.bean.Shop;
import com.hualan.dao.MenDianDao;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MenDianDaoImpl implements MenDianDao {
    @Override
    public List<Shop> searchMenDian(String shopName, String shopPhone, String startTime, String endTime) {
        List plist = new ArrayList();
        StringBuffer sql =  new StringBuffer("select * from panghu_shop where 1 = 1");
        if (shopName!= null && shopName.length() > 0){
            sql.append(" and shop_name like ? ");
            plist.add("%" + shopName + "%" );
            System.out.println(shopName);
        }
        if (shopPhone != null && shopPhone.length()> 0){
            sql.append(" and shop_phone like ? ");
            plist.add("%" + shopPhone + "%" );
            System.out.println(shopPhone);
        }
        if (startTime != null && startTime.length()> 0){
            sql.append(" and add_time >= ?");
            plist.add(startTime);
        }
        if (endTime != null && endTime.length()>= 0){
            sql.append(" and add_time <= ?");
            plist.add(endTime);
        }
        List<Shop> shopList = DBUtils.query(sql.toString(),Shop.class,plist.toArray());
        System.out.println(sql.toString());
        System.out.println("到层店名:" + shopName);
        return  shopList;
    }

    @Override
    public Shop selectMenDianById(int id) {
        Shop shop = DBUtils.queryOne("select * from panghu_shop where id = ?",Shop.class,id);

        return shop;
    }

    @Override
    public int addMenDian(Shop shop) {

        int row =DBUtils.update("insert into  panghu_shop(shop_name, shop_describe, shop_image,shop_phone, shop_province, shop_city, shop_district,shop_address, shop_longitude, shop_latitude,add_time) values(?,?,?,?,?,?,?,?,?,?,?)",shop.getShopName(),shop.getShopDescribe(),shop.getShopImage(),shop.getShopPhone(),shop.getShopProvince(),shop.getShopCity(),shop.getShopDistrict(),shop.getShopAddress(),shop.getShopLongitude(),shop.getShopLatitude(),shop.getAddTime());

        return row;
    }

    @Override
    public int updateMenDian(Shop shop) {
        int row =DBUtils.update("update panghu_shop set shop_name = ? , shop_describe = ?, shop_image = ?,shop_phone = ?, shop_province = ?, shop_city = ? , shop_district = ?,shop_address = ?, shop_longitude = ?, shop_latitude = ?, add_time = ? where shop_id = ?",shop.getShopName(), shop.getShopDescribe(),shop.getShopImage(),shop.getShopPhone(),shop.getShopProvince(),shop.getShopCity(),shop.getShopDistrict(),shop.getShopAddress(),shop.getShopLongitude(),shop.getShopLatitude(),shop.getAddTime(),shop.getShopId());
        return row;
    }

    @Override
    public Long selectAllMenDianCount() {
        Map<String,Object> map = DBUtils.queryOne("select count(*) as count from panghu_shop");
        Long count = (Long) map.get("count");
        return count;
    }

    @Override
    public List<Shop> selectMenDianWithPage(int page, int size) {
        List<Shop> shopList = DBUtils.query("select  * from panghu_shop limit ?,?",Shop.class, (page - 1) * size,size);
//        不止一本书籍
        for (Shop shop:shopList) {
//            System.out.println(shop);

        }
        return shopList;
    }
}
